package com.wh.minio.container;

import com.wh.minio.container.common.MinioClientUtils;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import io.minio.messages.Upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;


public class minioTest {

    public static  void main(String[] args){
        try {
            MinioClient minioClient = MinioClientUtils.getInstance().getMinioClient();

            List<Bucket> bucketList =  minioClient.listBuckets();
            for (Bucket bucket : bucketList) {
                System.out.println("AAA: " + bucket.creationDate() + ", " + bucket.name());
            }


            // Check whether 'mybucket' exists or not.
            boolean found = minioClient.bucketExists("open");
            if (found) {
                // List objects from 'my-bucketname'
                Iterable<Result<Item>> myObjects = minioClient.listObjects("open");
                for (Result<Item> result : myObjects) {
                    Item item = result.get();
                    System.out.println("BBB: " + item.lastModified() + ", " + item.size() + ", " + item.objectName() + "," + item.etag() );

                }
            } else {
                System.out.println("asiatrip does not exist");
            }


            // Check whether 'mybucket' exist or not.
            boolean exists = minioClient.bucketExists("asiatrip");
            if (exists) {
                // List all incomplete multipart upload of objects in 'my-bucketname
                Iterable<Result<Upload>> myObjects = minioClient.listIncompleteUploads("asiatrip");
                for (Result<Upload> result : myObjects) {
                    Upload upload = result.get();
                    System.out.println("CCC: " + upload.uploadId() + ", " + upload.objectName());
                }
            } else {
                System.out.println("asiatrip does not exist");
            }
            System.out.println("DDD: " + "Current policy: " + minioClient.getBucketPolicy("asiatrip"));



//            minioClient.statObject("asiatrip", "all json.txt");
//            minioClient.getObject("asiatrip", "open", "all json.txt");
//            // Get input stream to have content of 'my-objectname' from 'my-bucketname'
//            InputStream stream = minioClient.getObject("asiatrip", "open");
//            // Read the input stream and print to the console till EOF.
//            byte[] buf = new byte[16384];
//            int bytesRead;
//            while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
//                System.out.println("EEE: " + new String(buf, 0, bytesRead));
//            }
//
//            // Close the input stream.
//            stream.close();


            String url = minioClient.presignedGetObject("open", "xzp.jpg", 60 * 60 * 24);
            System.out.println("FFF:" + url);
            String url2 = minioClient.presignedPutObject("open", "xzp.jpg", 60 * 60 * 24 * 7);
            System.out.println("FFF:" + url2);




//            boolean isExist = minioClient.bucketExists("asiatrip");
//            if(isExist) {
//                System.out.println("BucketDTO already exists.");

//            } else {
//                minioClient.makeBucket("asiatrip");
//            }
//            minioClient.putObject("asiatrip","12.png", "E:\\12.png");
//            System.out.println("is successfully uploaded to `asiatrip` bucket.");
//            ObjectStat stat= minioClient.statObject("asiatrip","12.png");
//            System.out.println(stat);
//            minioClient.removeBucket("asiatrip");



              //FileInputStream fr = new FileInputStream("E://test-1.txt");


            File file = new File("H://ServiceDispatcherImpl.java");
            InputStream input = new FileInputStream(file);
            byte[] byt = new byte[input.available()];
            System.out.println("byt: " + byt + "byt.length: " + byt.length);
            input.read(byt);
            System.out.println("byt11: " + byt + "byt.length: " + byt.length);


        } catch(Exception e) {
            System.out.println("Error occurred: " + e);
        }
    }
}
